/*
 * Copyright (c) 2007 The Trustees of Indiana University. All rights reserved.
 * IU CSCI B534 Distributed Systems 2007-02-01 http://www.cs.indiana.edu/classes/b534
 */

package edu.indiana.cs.b534.torrent.struct;

import edu.indiana.cs.b534.torrent.FileDictionary;
import edu.indiana.cs.b534.torrent.InfoDictionary;
import edu.indiana.cs.b534.torrent.TDictionary;
import edu.indiana.cs.b534.torrent.TInteger;
import edu.indiana.cs.b534.torrent.TList;
import edu.indiana.cs.b534.torrent.TString;

import java.io.IOException;
import java.io.InputStream;

public class InfoDictionaryImpl extends TDictionaryImpl implements InfoDictionary {
	
	
	/**
	 * name-  suggested name to save the file (or directory)
	 * piece - length size of a  piece ,power of two,2^18 = 256 K (BitTorrent prior to version 3.2 uses 2^20 = 1 M as default).
	 * pieces - maps to a string whose length is a multiple of 20. It is to be subdivided into strings of length 20, each of which is the SHA1 hash of the piece at the corresponding index.
	 * length - single file length maps to the length of the file in bytes. OR
	 * files - [length,path] - the multi-file case is treated as only having a single file by concatenating the files in the order they appear in the files list.  
	 */
	
	public final static TString NAME_KEY = TStringImpl.newInstance("name");
	public final static TString PIECE_LENGTH = TStringImpl.newInstance("piece length");
	public final static TString PIECES_KEY = TStringImpl.newInstance("pieces");
	public final static TString LEANGTH_KEY = TStringImpl.newInstance("length");
	public final static TString FILES_KEY = TStringImpl.newInstance("files");
    
    private TString name;
    private TString pieces;
    private TInteger length;
    private TInteger peiceLength;
    private TList<FileDictionary> files;

    

    public static InfoDictionary deserialize(InputStream inStream) throws IOException {
    	TDictionary dictionary = TDictionaryImpl.deserialize(inStream);
    	return newInstanceFrom(dictionary);
    }

    public static InfoDictionary newInstance() {
        return new InfoDictionaryImpl();
    }
    public static InfoDictionary newInstanceFrom(TDictionary dictionary_) {
    	InfoDictionary infoDictionary =new InfoDictionaryImpl();
    	infoDictionary.setDictionary(dictionary_.getDictionary());
    	infoDictionary.setName((TString)(infoDictionary.getDictionary().get(NAME_KEY)));
    	infoDictionary.setPieceLength((TInteger)infoDictionary.getDictionary().get(PIECE_LENGTH));
    	infoDictionary.setLength((TInteger)infoDictionary.getDictionary().get(LEANGTH_KEY));
    	infoDictionary.setPieces((TString)(infoDictionary.getDictionary().get(PIECES_KEY)));
    	infoDictionary.setFiles((TList<FileDictionary>)(infoDictionary.getDictionary().get(FILES_KEY)));
    	return infoDictionary;
    }

    protected InfoDictionaryImpl() {}

    public TString getName() {
    	return name;
    }

    public TInteger getPieceLength() {
    	return peiceLength;
    }

    public TString getPieces() {
    	return pieces;
    }

    public TInteger getLength() {
    	return length;
    }

    public TList<FileDictionary> getFiles() {
    	return files;
    }


    public void setName(TString name_) {
		this.name = name_;
    }

    public void setPieceLength(TInteger pieceLength_) {
		this.peiceLength = pieceLength_;
    }

    public void setPieces(TString pieces_) {
		this.pieces = pieces_;
    }

    public void setLength(TInteger length_) {
		this.length = length_;
    }

    public void setFiles(TList<FileDictionary> files_) {
		this.files = files_;
    }

}
